<?php namespace App\Http\Services;

use Requests;

class ToolsManager
{
    /**
     * 生成订单编号
     * @return string
     */
    public static function makeBillNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . rand(100, 999);
        return $orderSn;
    }

    /**
     * 生成订单编号
     * @return string
     */
    public static function makeQzBillNum(){
        $orderSn = 'CZ'.date('Ymdhis',time()). substr(microtime(), 2, 3) . rand(100, 999);
        return $orderSn;
    }

    public static function makeRechargeNum() {
        $orderSn = 'cz' . date('YmdHis', time()) . substr(microtime(), 2, 4) . rand(100, 999);
        return $orderSn;
    }

    /**
     * 生城提取码
     * @return string
     */
    public static function makePickupCode(){
        $pickupCode = rand(100000,999999);
        return $pickupCode;
    }

    /**
     * 生成退款单号
     * @return string
     */
    public static function makeBRefundNum(){
        $orderSn = date('Ymdhis',time()). substr(microtime(), 2, 5) . rand(100, 999) . '@tui';
        return $orderSn;
    }

    /**
     * 求两个已知经纬度之间的距离,单位为米
     *
     * @param $lng1 ,$lng2 经度
     * @param $lat1 ,$lat2 纬度
     * @param $lng2
     * @param $lat2
     * @return float 距离，单位米
     */
    public static function getDistance($lng1, $lat1, $lng2, $lat2) {
        // 将角度转为狐度
        if ($lat1=='' || $lng1=='' || $lat2=='' || $lng2=='')
            return 0;
        $radLat1 = deg2rad($lat1); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        if ($s==0){
            $s=1;
        }
        $dis=round($s,2);
//        if ($dis>999){
            $dis=round($dis/1000,2).'km';
//        }else{
//            $dis=$dis.'m';
//        }
        return $dis;
    }

    /**
     * 求两个已知经纬度之间的距离,单位为米
     *
     * @param $address ,地址
     * @param $lng2 ,经度
     * @param $lat2 ,纬度
     * @return float 距离，单位米
     */
    public static function getDistanceByAddress($address, $lng2, $lat2) {
        // 将角度转为狐度
        if ($lat2=='' || $lng2=='')
            return 0;
        $key='6IHBZ-U3J3J-XPZF5-FAGPK-FHAPV-BBBMG';
        $sk ='JSaDiknBlyL6fnMLd7VGRefBis6qL3Ih';
        $url='https://apis.map.qq.com/ws/geocoder/v1?';
        $sign=md5('/ws/geocoder/v1?address='.$address.'&key='.$key.$sk);
        $request_url=$url.'address='.$address.'&key='.$key.'&sig='.$sign;
        $data = Requests::get($request_url);
        if (!$data->success)
            return 0;

        $location=$data->body;
        if (!$location)
            return 0;
        $location=json_decode($location);
        if ($location->status!=0)
            return 0;
//        dump($location->result->location) ;die();
        $radLat1 = deg2rad($location->result->location->lat); //deg2rad()函数将角度转换为弧度
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($location->result->location->lng);
        $radLng2 = deg2rad($lng2);
        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;
        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))) * 6378.137 * 1000;
        if ($s==0){
            $s=1;
        }
        $dis=round($s,2);
//        if ($dis>999){
//            $dis=round($dis/1000,2).'km';
//        }else{
//            $dis=$dis.'m';
//        }
        return $dis;
    }

    /**
     * 获取指定方圆距离的四个点
     * param lng float 经度
     * param lat float 纬度
     * param distance float 该点所在圆的半径，该圆与此正方形内切，默认值为单位米
     * return array 正方形的四个点的经纬度坐标
     * @param $lng
     * @param $lat
     * @param $distance
     * @return array
     */
    public static function getSquarePoint($lng, $lat,$distance)
    {
        $PI = 3.14159265;
        $longitude = $lng;
        $latitude = $lat;

        $degree = (24901*1609)/360.0;
        $raidusMile = $distance;

        $dpmLat = 1/$degree;
        $radiusLat = $dpmLat*$raidusMile;
        $minLat = $latitude - $radiusLat;       //拿到最小纬度
        $maxLat = $latitude + $radiusLat;       //拿到最大纬度

        $mpdLng = $degree*cos($latitude * ($PI/180));
        $dpmLng = 1 / $mpdLng;
        $radiusLng = $dpmLng*$raidusMile;
        $minLng = $longitude - $radiusLng;     //拿到最小经度
        $maxLng = $longitude + $radiusLng;     //拿到最大经度
        $range = array(
            'minLat' => $minLat,
            'maxLat' => $maxLat,
            'minLng' => $minLng,
            'maxLng' => $maxLng
        );
        return $range;
    }

    public static function coverString($string){
        if ($string=='')
            return '';
        $string =trim($string);
        $first4 =mb_substr($string,0,4,'utf8');
        $end2   =mb_substr($string,-2,2,'utf8');
        $result =$first4.'*'.$end2;
        return $result;
    }

    public static function getDeliveryId(int $merchant_id,int $order_id,int $times = 1): int
    {
        //1001001
        $delivery_id = $merchant_id.'00'.$order_id.'00'.$times;
        $delivery_id = (int)$delivery_id;
        if ($delivery_id < 1002)
            return time();
        return $delivery_id;
    }
}
